package com.company.project.robot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.robot.entity.JokesEntity;
import com.company.project.robot.service.JokesService;
import com.company.project.robot.vo.ReptilesVo;
import com.company.project.sys.service.RedisService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.io.IOException;
import java.util.List;
import com.company.project.common.utils.DataResult;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 笑话表
 *
 * @author zyf
 * @email 2102378781@qq.com
 * @date 2021-11-03 09:54:38
 */
@Controller
@RequestMapping("/")
@Api(tags = "笑话表")
public class JokesController {
    @Autowired
    private JokesService jokesService;

    @Autowired
    private RedisService redisService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/jokes")
    public String jokesList() {
        return "jokes/list";
    }

    /**
    * 跳转到修改页面
    */
    @GetMapping("/index/jokes/info")
    public String jokesInfo(String id, Model model) {
        model.addAttribute("id",id);
        return "jokes/edit";
    }

    /**
     * 跳转至爬取页面
     */
    @GetMapping("/index/jokes/reptile")
    public String jokesReptile() {
        return "jokes/reptile";
    }


    @ApiOperation(value = "新增")
    @PostMapping("jokes/add")
    @ResponseBody
    public DataResult add(@RequestBody JokesEntity jokes){
            jokesService.save(jokes);
        redisService.del("jokes");
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("jokes/delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
            jokesService.removeByIds(ids);
        redisService.del("jokes");
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("jokes/update")
    @ResponseBody
    public DataResult update(@RequestBody JokesEntity jokes){
            jokesService.updateById(jokes);
        redisService.del("jokes");
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("jokes/listByPage")
    @ResponseBody
    public DataResult findListByPage(@RequestBody JokesEntity jokes){
        Page page = new Page(jokes.getPage(), jokes.getLimit());
        LambdaQueryWrapper<JokesEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        if(StrUtil.isNotEmpty(jokes.getTEXT())){
            queryWrapper.like(JokesEntity::getTEXT, jokes.getTEXT());
        }
        IPage<JokesEntity> iPage = jokesService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @GetMapping("/jokes/{id}")
    @ApiOperation(value = "查询详情接口")
    @ResponseBody
    public DataResult detailInfo(@PathVariable("id") String id) {
        return DataResult.success(jokesService.getById(id));
    }


    @ApiOperation(value = "导出模板")
    @GetMapping(value = "/jokes/export/exportExcel",  produces = { "application/json;charset=UTF-8" })
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        jokesService.exportExcel(request,response);
    }

    @ApiOperation(value = "导入模板")
    @PostMapping(value = "/jokes/export/excelImport",  produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public DataResult excelImport(@RequestParam("file") MultipartFile file) throws Exception {
        return DataResult.success(jokesService.excelImport(file));
    }

    @ApiOperation(value = "爬虫爬取中关村数据接口")
    @PostMapping(value = "/jokes/riddle",  produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public DataResult riddle(@RequestBody ReptilesVo reptilesVo) throws IOException {
        redisService.del("jokes");
        jokesService.listSave(reptilesVo);

        return DataResult.success();
    }
}
